package easy.netty.net.core;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author cengy
 */
public class IoMgr {

    public IoMgr() {
    }

    protected ConcurrentMap<String, Entity>
            entityMap = new ConcurrentHashMap<>();

    public void add(Entity entity) {
        if (null == entity) {
            return;
        }

        entityMap.putIfAbsent(entity.getId(), entity);
    }

    public void remove(Entity entity) {
        if (null == entity) return;
        entityMap.remove(entity.getId());
    }

    public boolean has(Entity entity) {
        return null == entity ? false : entityMap.containsKey(entity.getId());
    }

    public List<Entity> getAll() {
        return new ArrayList<>(entityMap.values());
    }

    public Entity get(String id) {
        if (null == id || "".equals(id))
            return null;
        return entityMap.get(id);
    }
}
